#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/* 	Calculating the pi value using gauss-legenre method	*/

int main (int argc, char ** argv)
{
	if(argc != 2) {
		printf("Usage: ./gauss-legendre-seq <number_of_iteractions>\n");
		exit(1);
	}

	/* 	initializating the variables	*/

	//	number of iteractions
	int n = atoi(argv[1]);	

	//	all terms used by the gauss-legendre method
	double a = 1.0, b = 1.0 / sqrt(2), t = 0.25, p = 1.0;
	double a1, b1, t1, p1;
	
	int i;
	for(i = 0; i < n; i++) {

		//	for each iteraction calculate all new terms
		a1 = (a + b) / 2.0;
		b1 = sqrt(a * b);
		t1 = t - p * (a - a1) * (a - a1);
		p1 = 2.0 * p;

		a = a1, b = b1, t = t1, p = p1;
	}
		
	//	at the end, calculate the value of Pi
	double pi = ((a1 + b1) * (a1 + b1)) / (4.0 * t1);
	printf("%.7lf\n", pi);

	return 0;
}
